
package com.craterhill.java.ems.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.craterhill.java.ems.common.beans.Position;
import com.craterhill.java.ems.dao.PositionDao;

@Repository
public class PositionDaoImpl extends AbstractDao<Position> implements PositionDao {

	@Autowired
	public PositionDaoImpl(final DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public int save(Position position) {
		return super.save(new PositionPreparedStatementCreator(ACTION_SAVE, position));
	}

	@Override
	public boolean update(Position position) {
		return super.update(new PositionPreparedStatementCreator(ACTION_UPDATE, position));
	}

	@Override
	public boolean remove(final Integer... positionIds) {
		return super.batchOperation(DELETE_POSITION, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				preparedStatement.setInt(1, positionIds[position]);
			}

			@Override
			public int getBatchSize() {
				return positionIds.length;
			}
		});
	}

	@Override
	public List<Position> findAll() {
		return super.findMultipleObject(FIND_ALL_POSITION, new PositionExtractor());
	}

	@Override
	public Position find(int recordId) {
		return super.findSingleObject(FIND_POSITION, new PositionExtractor(), recordId);
	}
}
